const db = require('../db/index')

// 发布订单
exports.releaseOrderHandlrt = (req, res) => {
    const order = req.body
    order.create_time = new Date().toLocaleString()
    order.order_status = 1
    order.user_id = req.user.id

    const sql = 'insert into orderinfo set?'
    db.query(sql, order, (err, result) => {
        if(err) res.cc(err)
        if(result.affectedRows > 0) {
           return res.cc('发布成功',0)
        } else {
           return res.cc('发布失败')
        }
    })
}

// 获取用户订单列表
exports.getOrderListHandler = (req, res) => {
    const sql = `SELECT count(*) as total FROM orderinfo WHERE user_id =?`
    db.query(sql, req.user.id, (err, result) => {
        if(err) res.cc(err)
        if(result.length === 0) {
           return res.cc('暂无订单',0)
        }
        const total = result[0].total
        const sql =
        `SELECT 
            o.*, m.nickname, s.service_name, s.service_img, s.service_pice 
        FROM 
            orderinfo o, maintainer m,repair_service s
        WHERE 
            o.user_id=? 
        AND (
            o.maintainer_id = m.id OR o.maintainer_id IS NULL
        ) 
        AND 
            o.service_id = s.id
        GROUP BY 
            id
        order by
            o.appointmentDate, o.startTime asc
        LIMIT ?,?`
        const pageSize = Number(req.query.pageSize)
        const currentPage = Number(req.query.currentPage)
        db.query(sql, [req.user.id,currentPage,pageSize], (err, result) => {
            if(err) res.cc(err)
            if(result.length === 0) {
               return res.cc('暂无订单',0)
            }
            res.send({
                status:0,
                message:'获取成功',
                total: total,
                data: result
            })
        })
    })
}


//  根据不同的订单状态获取订单列表
exports.getOrderListByStatusHandler = (req, res) => {
    const sql = `SELECT count(*) as total FROM orderinfo WHERE user_id =? AND order_status =?`
    db.query(sql, [req.user.id, req.query.status], (err, result) => {
        if(err) res.cc(err)
        if(result.length === 0) {
           return res.cc('暂无订单',0)
        }
        const total = result[0].total
        const sql =
        `SELECT 
            o.*, m.nickname,s.service_name, s.service_img, s.service_pice 
        FROM 
            orderinfo o, maintainer m,repair_service s
        WHERE 
            o.user_id=? 
        AND (
            o.maintainer_id = m.id OR o.maintainer_id IS NULL
        ) 
        AND 
            o.service_id = s.id 
        AND 
            o.order_status =?
        GROUP BY 
            id
        order by 
            o.appointmentDate, o.startTime asc
        LIMIT ?,?`
        const pageSize = Number(req.query.pageSize)
        const currentPage = Number(req.query.currentPage)
        db.query(
            sql,
            [req.user.id, req.query.status, currentPage, pageSize],
            (err, result) => {
                if(err) res.cc(err)
                if(result.length === 0) {
                    return res.cc('暂无订单',0)
                }
                // console.log(result);
                res.send({
                    status:0,
                    message:'获取成功',
                    total: total,
                    data: result
                })
            }
        )
    })

}

// 获取订单详细
exports.getOrderDetailHandler = (req, res) => {
    const sql = `SELECT
	o.appointmentDate,
	o.startTime,
    o.endTime,
    o.linkman,
    o.phone,
	o.failure,
    o.remark,
    o.order_status,   
	o.create_time,
	o.take_time,
	o.done_time,
	o.maintainer_id,
	m.nickname as'maintainer_nickname',	
	m.phone as 'maintainer_phone',
	s.service_name,
	s.service_img,
	s.service_pice,
	a.districts,
	a.city,
	a.county,
	a.full_address
FROM
	orderinfo o,
	maintainer m,
	repair_service s,
  address a
WHERE
	o.id = ?
AND
	o.serviceAddress = a.id
AND
 (o.maintainer_id = m.id OR o.maintainer_id IS NULL)
AND
	o.service_id = s.id`
    db.query(sql, req.query.id, (err, result) => {
        if(err) res.cc(err)
        if(result.length === 0) {
           return res.cc('获取失败')
        }
        res.send({
            status:0,
            message:'获取成功',
            data: result[0]
        })
    })
}

// 支付订单
exports.payOrderHandler = (req, res) => {
    req.body.done_time = new Date().toLocaleString()
    const sql = `UPDATE orderinfo SET ? WHERE id = ?`
    db.query(sql, [req.body, req.body.id], (err, result) => {
        if(err) res.cc(err)
        if(result.affectedRows > 0) {
           return res.cc('支付成功',0)
        } else {
           returnres.cc('支付失败')
        }
    })
}
// 评价订单
exports.evaluateOrderHandler = (req, res) => {
    const sql = `UPDATE orderinfo SET ? WHERE id = ?`
    db.query(sql, [req.body, req.body.id], (err, result) => {
        if(err) res.cc(err)
        if(result.affectedRows > 0) {
           return res.cc('评价成功',0)
        } else {
           return res.cc('评价失败')
        }
    })
}